/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.plugins.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.plugins.VoteDao;
import org.grayrabbit.cms.entity.Vote;
import org.grayrabbit.cms.vo.plugins.VoteBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>Vote</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 16:08:34 
 */
@Repository
public class VoteDaoImpl extends GenericHibernateDao<Vote, String>
		implements VoteDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From Vote as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see VoteDao#assembly(VoteBean)
	 */
	public QueryCondition assembly(VoteBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see VoteDao#assemblyCount(VoteBean)
	 */
	public QueryCondition assemblyCount(VoteBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see VoteDao#count(VoteBean)
	 */
	public int count(VoteBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see VoteDao#assemblyNameQuery(VoteBean)
	 */
	public QueryCondition assermblyNameQuery(VoteBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see VoteDao#assemblyNameQueryCount(VoteBean)
	 */
	public QueryCondition assemblyNameQueryCount(VoteBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see VoteDao#findByPage(Page, VoteBean)
	 */
	public List<Vote> findByPage(Page<Vote> page,
			VoteBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		qc.append(" order by obj.ord desc");
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(VoteBean bean,QueryCondition qc){
		if(bean!=null){
			if(StringUtils.isNotBlank(bean.getVoteTitle())){
				qc.add("And obj.voteTitle like ? ","%"+ bean.getVoteTitle()+"%");
			}
            if(bean.getVoteStatus() != null){
				qc.add("And obj.voteStatus = ? ", bean.getVoteStatus());
			}
          
            if(StringUtils.isNotBlank(bean.getCreateRealName())){
				qc.add("And obj.createRealName like ? ","%"+ bean.getCreateRealName()+"%");
			}
          
            if(StringUtils.isNotBlank(bean.getSite().getSiteId())){
            	qc.add(" And obj.site.siteId = ? ", bean.getSite().getSiteId());
            }
		}
	}
	/**
	 * 检查唯一性
	 */
	public Boolean checkNameUnique(String voteTitle,String votename,String siteId){
		boolean unique=false;
		List<Vote> voteList=null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(voteTitle)){
			qc.add(" And obj.voteTitle =?", voteTitle);
		}
		if(StringUtils.isNotBlank(siteId)){
			qc.add(" And obj.site.siteId = ?", siteId);
		}
		voteList=findByQc(qc);
		if(StringUtils.isNotBlank(votename) && !voteList.isEmpty()){
			for(int i=0;i<voteList.size();i++){
				if(voteList.get(i).getVoteTitle().equals(votename)){
					voteList.remove(i);
				}
			}
		}
		if((voteList!=null)&&(!voteList.isEmpty())){
			unique=true;
		}
		return unique;
	}
	
	public List<Vote> voteList(String siteId){
		List <Vote> voteList = null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(siteId)){
			qc.add(" And obj.site.siteId = ? ", siteId);
		}else{
			qc.append("And obj.site.siteId is null");
		}
		qc.append(" order by obj.ord desc");
		voteList = findByQc(qc);
		return voteList;
	}
	/**
	 * 根据站点ID查询
	 * @return
	 */
	public List<Vote>findBySiteId(String siteId){
		QueryCondition qc = assembly(null);
		if(StringUtils.isNotBlank(siteId)){
			qc.add("And obj.site.siteId = ? ", siteId);
		}
		qc.append(" And obj.voteStatus = 1");
		qc.append(" order by obj.createTime desc");
		return findByQc(qc);
	}
	public List<Vote>findBySiteId2(String siteId,String voteId){
		QueryCondition qc = assembly(null);
		if(StringUtils.isNotBlank(siteId)){
			qc.add("And obj.site.siteId = ? ", siteId);
		}
		if(StringUtils.isNotBlank(voteId)){
			qc.add(" And obj.voteId <> ?", voteId);
		}
		qc.append(" And obj.voteStatus = 1");
		qc.append(" order by obj.createTime desc");
		return findByQc(qc);
	}
}